import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/forms/forms.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:reactive_forms/reactive_forms.dart';
import '../../utils/log.dart';

class ResetTeacherEmailDialog extends StatelessWidget {
  ResetTeacherEmailDialog({
    Key? key,
    required this.userName,
  }) : super(key: key);
  final String userName;

  @override
  Widget build(BuildContext context) {
    Log.debug("build ResetTeacherEmailDialog username=${userName.toString()}");

    final UserController _usercontroller = Get.find();
    ChangeEmailFormModel formModel = new ChangeEmailFormModel();
    FormGroup form = formModel.form;

    void _onUpdate() {
      Log.debug('_onUpdate');
      Log.debug(form.value);
      if (!form.valid) {
        form.markAllAsTouched();
        return;
      }
      String email = form.control("email").value;
      _usercontroller
          .resetTeacherEmail(this.userName, email)
          .then((statusMessage) {
        if (statusMessage.status == 1000) {
          showSnackMsg('Successfully updated!');
          Navigator.of(Get.context!).pop();
        } else {
          showSnackMsg('Failed to update!');
        }
      });
    }

    void _onCancel() {
      Navigator.of(context).pop();
    }

    _buildForm() {
      List<Widget> formWidget = [];

      formWidget.add(ReactiveTextField(
        formControlName: 'email',
        style: Theme.of(context).textTheme.displaySmall,
        textInputAction: TextInputAction.next,
        decoration: InputDecoration(
          alignLabelWithHint: true,
          labelText: 'Email'.tr,
          labelStyle: Theme.of(context).textTheme.displaySmall,
        ),
      ));
      formWidget.add(ReactiveTextField(
        formControlName: 'confirmemail',
        style: Theme.of(context).textTheme.displaySmall,
        textInputAction: TextInputAction.next,
        decoration: InputDecoration(
          alignLabelWithHint: true,
          labelText: 'Confirm Email'.tr,
          labelStyle: Theme.of(context).textTheme.displaySmall,
        ),
      ));

      formWidget.add(SizedBox(
        height: 24,
      ));
      formWidget.add(Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          ReactiveFormConsumer(
            builder: (context, form, child) {
              return ElevatedButton(
                  child: Text('Cancel'.tr), onPressed: _onCancel);
            },
          ),
          SizedBox(width: 20),
          ReactiveFormConsumer(
            builder: (context, form, child) {
              return ElevatedButton(
                  child: Text('Submit'.tr), onPressed: _onUpdate);
            },
          ),
        ],
      ));
      return formWidget;
    }

    _buildObxForm() {
      return ReactiveForm(
          formGroup: form, child: Column(children: _buildForm()));
      // return Obx(() {
      //   return ReactiveForm(
      //       formGroup: form, child: Column(children: _buildForm()));
      // });
    }

    return AlertDialog(
        title: Text('Change Email'.tr),
        titleTextStyle: TextStyle(
            fontWeight: FontWeight.bold,
            letterSpacing: 1,
            color: Get.isDarkMode ? Color(0xffcccccc) : Colors.black,
            fontSize: 20),
        backgroundColor: Get.isDarkMode
            ? (Color(0xff333333))
            : Color.fromARGB(255, 234, 241, 238),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(20))),
        content: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Container(
                width: 420,
                height: 268,
                child: Column(children: [_buildObxForm()]))));
  }
}
